#include "class_strategy_energy.h"
#include "class_center.h"
#include "class_strategy_position.h"

extern class_center a;
extern lambda_pool pool;


class_strategy_energy::class_strategy_energy(int idd, std::string dm, int n1, double fd, int sl)
{
memset(&ints,0,sizeof(ints));
memset(&dous,0,sizeof(dous));

memset(&tick,0,sizeof(tick));

name = dm;

std::string str = a.path_str + "/strategy";
//if(nullptr == opendir(str.c_str()))
//{
//auto return_value=mkdir(str.c_str(),777);
//if(return_value != 0)
//{
//	std::cout<<"建立目录:"<<str<<"失败！返回值:"<<return_value<<std::endl;
//}
//}
create_path(str);

str = a.path_str + "/strategy/energy";
//if(nullptr == opendir(str.c_str()))
//{
//auto return_value=mkdir(str.c_str(),777);
//if(return_value != 0)
//{
//	std::cout<<"建立目录:"<<str<<"失败！返回值:"<<return_value<<std::endl;
//}
//}
create_path(str);

str = a.path_str + "/strategy/energy/"+dm;
//if(nullptr == opendir(str.c_str()))
//{
//auto return_value=mkdir(str.c_str(),777);
//if(return_value != 0)
//{
//	std::cout<<"建立目录:"<<str<<"失败！返回值:"<<return_value<<std::endl;
//}
//}
create_path(str);

str = a.path_str + "/log/strategy";
//if(nullptr == opendir(str.c_str()))
//{
//auto return_value=mkdir(str.c_str(),777);
//if(return_value != 0)
//{
//	std::cout<<"建立目录:"<<str<<"失败！返回值:"<<return_value<<std::endl;
//}
//}
create_path(str);

str = a.path_str + "/log/strategy/energy";
//if(nullptr == opendir(str.c_str()))
//{
//auto return_value=mkdir(str.c_str(),777);
//if(return_value != 0)
//{
//	std::cout<<"建立目录:"<<str<<"失败！返回值:"<<return_value<<std::endl;
//}
//}
create_path(str);

ints[1] = n1;
ints[2]=sl;
dous[1] = fd;
cc = new class_strategy_position(a.path_str + "/strategy/energy/" + dm);

ticks.read(a.path_str+"/ticks/" + name + ".dat", n1+2);
if(a.ticks_map.find(dm) != a.ticks_map.end() )
{
for(auto &v: a.ticks_map[dm].datas)
{
ticks.datas.push_back(v);
}
}

//计算
while(ticks.datas.size() > ints[1] + 2)
{
	ticks.datas.pop_front();
}
if(ticks.datas.size() > ints[1] )
{
for(auto it = ticks.datas.end() - ints[1]; it != ticks.datas.end(); ++it)
{
	if(it->Volume >= (it-1)->Volume )
	{
		dous[5] = it->Volume - (it-1)->Volume;
	}
	else
	{
dous[5] = it->Volume;
	}
	dous[6] += dous[5];
		if( (it->LastPrice > (it-1)->LastPrice && it->OpenInterest > (it-1)->OpenInterest) || (it->LastPrice < (it-1)->LastPrice && it->OpenInterest < (it-1)->OpenInterest) )
	{
			dous[7] += dous[5];
		}
		else	if( (it->LastPrice < (it-1)->LastPrice && it->OpenInterest > (it-1)->OpenInterest) || (it->LastPrice > (it-1)->LastPrice && it->OpenInterest < (it-1)->OpenInterest) )
	{
			dous[7] -= dous[5];
	}
}
	dous[8] = dous[6] != 0 ?dous[7] / dous[6] : 0;
	dous[6] = 0.0;
	dous[7] = 0.0;
	}

id=idd;
a.tick_event_map[name].insert([this](){ pool.insert([this](){ if(a.flag[50]==1){run_one();} }); },id);
a.log.insert("strategy/energy/"+name,"实例化成功,当前仓位" + std::to_string(cc->position));
}

class_strategy_energy::~class_strategy_energy()
{
a.tick_event_map[name].pop(id);
	a.log.insert("strategy/energy/"+name,"析构完成,当前仓位"+std::to_string(cc->position));
delete cc;
}


// 主执行函数
void class_strategy_energy::run_one()
{


//清理报单返回值
a.return_erase(*cc);
//同步持仓
a.position_sync(*cc);
//获取到TICK
tick=a.tick_map[name];
ticks.insert(tick);

//下面计算
if(ticks.datas.size() <  ints[1] + 2)
{
	return;
}

for(auto it = ticks.datas.end() - ints[1]; it != ticks.datas.end(); ++it)
{
	if(it->Volume >= (it-1)->Volume )
	{
		dous[5] = it->Volume - (it-1)->Volume;
	}
	else
	{
dous[5] = it->Volume;
	}
	dous[6] += dous[5];
		if( (it->LastPrice > (it-1)->LastPrice && it->OpenInterest > (it-1)->OpenInterest) || (it->LastPrice < (it-1)->LastPrice && it->OpenInterest < (it-1)->OpenInterest) )
	{
			dous[7] += dous[5];
		}
		else	if( (it->LastPrice < (it-1)->LastPrice && it->OpenInterest > (it-1)->OpenInterest) || (it->LastPrice > (it-1)->LastPrice && it->OpenInterest < (it-1)->OpenInterest) )
	{
			dous[7] -= dous[5];
	}
}
	dous[8] = dous[6] != 0 ?dous[7] / dous[6] : 0;
	dous[6] = 0.0;
	dous[7] = 0.0;
ticks.datas.pop_front();

//如果处于买入区且仓位不够则
if (dous[8] > dous[1] && cc->position < ints[2] && cc->return_vector.size() == 0)
{
a.bd_auto(tick.InstrumentID, '0', tick.UpperLimitPrice, 1, *cc);
a.log.insert("strategy/energy/" + name +"交易", name + "多开动作" + std::to_string(tick.AskPrice1));
return;
}

if (dous[8] < - dous[1] && cc->position > - ints[2] && cc->return_vector.size() == 0)
{
a.bd_auto(tick.InstrumentID, '1', tick.LowerLimitPrice, 1, *cc);
a.log.insert("strategy/energy/" + name + "交易", name + "空开动作" + std::to_string(tick.BidPrice1));
return;
}

if (dous[8] < 0 && cc->position > 0 && cc->return_vector.size() == 0)
{
a.bd_auto(tick.InstrumentID, '1', tick.LowerLimitPrice, 1,*cc);
a.log.insert("strategy/energy/" + name + "交易", name + "多头止损" + std::to_string(tick.BidPrice1));
return;
}

if (dous[8] > 0 && cc->position < 0 && cc->return_vector.size() == 0)
{
a.bd_auto(tick.InstrumentID, '0', tick.UpperLimitPrice, 1, *cc);
a.log.insert("strategy/energy/" + name + "交易", name + "空头止损" + std::to_string(tick.BidPrice1));
}

return;
}
